<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="./js/vue.js"></script>
    <title>自定义指令 v2</title>
  </head>
  <body>
    <div id="root">
      <input type="text" v-add-number:value="n" disabled />
      <button @click="n++">增加</button>
    </div>
    <script>
      // 全局 Vue.directives(指令名，对象或函数)
      new Vue({
        el: "#root",
        data: {
          n: 1,
        },
        // 避坑：所有指令当中的this值window
        directives: {
          // 对象式
          "add-number": {
            // 指令和元素成功绑定时
            bind(ele, binding) {
              ele.value = binding.value;
            },
            inserted(ele, binding) {
              ele.focus(); // 指令所在元素被插入页面时 页面加载时
            },
            // 指令所在模板重新解析时
            update(ele, binding) {
              ele.value = binding.value;
            },
          },
        },
      });
    </script>
  </body>
</html>
